<?php
namespace app\store\controller\user;
use app\store\controller\Controller;
use app\common\model\user\LabelReward as RewardModel;
use app\store\model\user\UserLabel;

class LabelReward extends Controller{

    public function index($label_id=''){
        $model = new RewardModel;
        $list = $model->getList($label_id);
        $name=UserLabel::where(['label_id'=>$label_id])->value('name');
        return $this->fetch('index', compact('list','name'));
    }

    /**
     * 添加
     * @return array|mixed
     */
    public function add($label_id='')
    {
        $model = new RewardModel;
        if (!$this->request->isAjax()) {
            // 文章分类
            return $this->fetch('add');
        }
        // 新增记录
        if ($model->add($this->postData('reward'))) {
            return $this->renderSuccess('添加成功', url('user.labelReward/index',['label_id'=>$label_id]));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }



    /**
     * 更新文章
     * @param $id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($id,$label_id)
    {
        // 详情
        $model = RewardModel::detail($id);
        if (!$this->request->isAjax()) {
            // 文章分类
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('reward'))) {
            return $this->renderSuccess('更新成功', url('user.labelReward/index',['label_id'=>$label_id]));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 删除
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($id)
    {

        $model = RewardModel::detail($id);
        $res=$model->where(['reward_id'=>$id])->delete();

        if (!$res) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}